<template>
    <div class="question-bank-container">
        <!-- 标题区域 -->
        <div class="page-header">
            <el-text class="title" size="large" tag="b">
                <el-icon class="icon">
                    <Collection />
                </el-icon>
                题库列表
            </el-text>
            <el-text type="info" size="small">共 {{ total }} 个题库资源</el-text>
        </div>

        <!-- 加载骨架屏 -->
        <el-skeleton :loading="loading" animated :count="6" :throttle="500">
            <template #template>
                <el-row :gutter="16">
                    <el-col v-for="i in 6" :key="i" :xs="24" :sm="12" :md="8" :lg="8">
                        <el-skeleton-item variant="image" style="height: 120px" />
                        <el-skeleton-item variant="h3" style="width: 70%; margin-top: 12px" />
                        <el-skeleton-item variant="text" style="width: 90%" />
                    </el-col>
                </el-row>
            </template>

            <!-- 正式内容 -->
            <template #default>
                <!-- 错误提示 -->
                <el-alert v-if="error" :title="error" type="error" show-icon closable class="mb-4" />

                <!-- 卡片列表 -->
                <el-row :gutter="16">
                    <el-col v-for="item in records" :key="item.id" :xs="24" :sm="12" :md="8" :lg="8" class="card-col">
                        <el-card shadow="hover" :body-style="{ padding: '0' }" @click="router.push({name: 'bank-detail',params:{id:item.id}})" class="question-card">
                            <!-- 封面图 -->
                            <el-image :src="item.picture.replace(/\n/g, '')" fit="cover" class="cover-image"
                                :preview-src-list="[item.picture]" @error="handleImageError">
                                <template #error>
                                    <div class="image-error">
                                        <el-icon :size="32">
                                            <Picture />
                                        </el-icon>
                                    </div>
                                </template>
                            </el-image>

                            <!-- 卡片内容 -->
                            <div class="card-content">
                                <!-- 标题区 -->
                                <div class="card-header">
                                    <el-text class="card-title" truncate>{{ item.title }}</el-text>
                                    <el-text type="info" size="small" class="create-time">
                                        <el-icon>
                                            <Clock />
                                        </el-icon>
                                        {{ formatDate(item.createTime) }}
                                    </el-text>
                                </div>

                                <!-- 描述 -->
                                <el-text class="description" truncated :line-clamp="2">
                                    {{ item.description }}
                                </el-text>

                                <!-- 用户信息 -->
                                <div class="user-info">
                                    <el-avatar :size="32" :src="item.user.userAvatar" class="user-avatar">
                                        <el-icon>
                                            <User />
                                        </el-icon>
                                    </el-avatar>
                                    <div class="user-meta">
                                        <el-text size="small" strong>{{ item.user.userName }}</el-text>
                                        <el-text type="info" size="small">{{ item.user.userProfile }}</el-text>
                                    </div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>

                <!-- 分页 -->
                <div class="pagination" v-if="records.length > 0">
                    <el-pagination background layout="prev, pager, next" :total="Number(total)"
                        :page-size="Number(size)" :current-page="Number(current)" @current-change="handlePageChange" />
                </div>
            </template>
        </el-skeleton>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import axios from 'axios'
import { Clock, Picture, User, Collection } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
// 响应式数据
const records = ref([])
const loading = ref(true)
const error = ref(null)
const total = ref(0)
const size = ref(10)
const current = ref(1)
const router = useRouter()
// 处理图片错误
const handleImageError = (e) => {
    e.target.src = 'https://www.code-nav.cn/logo.png'
}

// 日期格式化
const formatDate = (dateString) => {
    return new Date(dateString).toLocaleDateString()
}

// 分页变化
const handlePageChange = (newPage) => {
    current.value = newPage
    fetchQuestionBank()
}

// 获取数据
const fetchQuestionBank = async () => {
    try {
        loading.value = true
        const response = await axios.post('http://localhost:8121/api/questionBank/list/page/vo', {
            current: current.value,
            pageSize: size.value,
        })

        // 更新响应数据
        records.value = response.data.data.records
        total.value = response.data.data.total
        size.value = response.data.data.size
        current.value = response.data.data.current
    } catch (err) {
        error.value = err.message || '请求失败，请稍后重试'
    } finally {
        loading.value = false
    }
}

onMounted(fetchQuestionBank)
</script>

<style scoped>
.question-bank-container {
    background-color: rgb(245, 245, 245);
    min-height: 100vh;
    padding: 20px;
    max-width: 1200px;
    margin: 0 auto;
}

/* 标题区域 */
.page-header {
    margin: 0 0 24px 8px;
    display: flex;
    align-items: baseline;
    gap: 16px;

    .title {
        color: var(--el-color-primary);
        font-size: 20px;

        .icon {
            margin-right: 8px;
            vertical-align: -2px;
        }
    }
}

/* 卡片容器 */
.card-col {
    padding: 8px !important;
}

.question-card {
    border-radius: 8px;
    transition: all 0.2s;
    margin-bottom: 12px;

    &:hover {
        transform: translateY(-3px);
        box-shadow: var(--el-box-shadow-dark);
    }
}

.cover-image {
    width: 100%;
    height: 120px;
    border-radius: 8px 8px 0 0;
}

.image-error {
    height: 120px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--el-fill-color-light);
}

.card-content {
    padding: 12px;
}

.card-header {
    margin-bottom: 8px;

    .card-title {
        font-size: 15px;
        display: block;
        margin-bottom: 4px;
    }

    .create-time {
        display: flex;
        align-items: center;
        gap: 4px;
    }
}

.description {
    color: var(--el-text-color-regular);
    font-size: 13px;
    line-height: 1.4;
    margin: 8px 0;
    min-height: 36px;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    margin-top: 8px;

    .user-avatar {
        flex-shrink: 0;
    }

    .user-meta {
        line-height: 1.3;
    }
}

.pagination {
    margin-top: 24px;
    display: flex;
    justify-content: center;
}
</style>